home *** CD-ROM | disk | FTP | other *** search
/ The Essential Home & Business Collection / The Essential Home & Business Collection.iso / 27 / 3 / 5 / HP22D5.ZIP / EXTERN / LOWIO.C < prev    next >
Text File  |  1991-04-16  |  5KB  |  238 lines

  1. /*
  2. ** This file implements the following DOS level I/O routines:
  3. **
  4. **    open        (lopen)
  5. **    close        (lcreate)
  6. **    read        (lread)
  7. **    write        (lwrite)
  8. **    filelength    (lfilelength)
  9. **    tell        (ltell)
  10. **    seek         (lseek)
  11. **
  12. ** The routines use MS-DOS file numbers to refer to files. These are the same
  13. ** number that HyperPAD uses internally to refer to files; so these routines
  14. ** can be inter-mixed with HyperPAD file routines.
  15. */
  16. #include "extern.h"
  17. #include "io.h"
  18.  
  19. /*
  20. ** This routine opens a file and returns its file number to HyperPAD. -1 will
  21. ** be returned if the open wasn't successful.
  22. **
  23. ** The function takes two parameters:
  24. **
  25. **    filename        DOS file name, like "C:\DATA.DAT" or "data.dat"
  26. **    mode            "w"    open for write mode
  27. **                "r"    open for read-only mode
  28. **                "wr"    open for read and write
  29. **
  30. **                    This parameter is case sensitive
  31. **
  32. ** To call this function from HyperPAD:
  33. **
  34. **    get lopen("data.dat","r");
  35. **    if it is -1 then
  36. **      answer "Couldn't open the file." with "Ok";
  37. */
  38. lopen(NumArgs,hFileName,hMode)
  39.  
  40. int NumArgs;
  41. HANDLE hFileName;
  42. HANDLE hMode;
  43.  
  44. {
  45.     PTR p;
  46.     int mode = READ;
  47.  
  48.     if (NumArgs != 2) return(STOP);
  49.  
  50.     p = deref(hMode);
  51.     if (*p == 'r') {
  52.         if (*(p + 1) == 'w') mode = READ_WRITE;
  53.     }
  54.     else if (*p == 'w') {
  55.         if (*(p + 1) == 'r') mode = READ_WRITE;
  56.         else mode = WRITE;
  57.     }
  58.  
  59.     ReturnValue(itoh(open(htos(hFileName),mode)));
  60.  
  61.     return(STOP);    
  62. }
  63.  
  64. /*
  65. ** This routine creates a file and returns its file number to HyperPAD.
  66. **
  67. ** To call this function from HyperPAD:
  68. **
  69. **    get lcreate("data.dat");
  70. **    if it is -1 then
  71. **      answer "Couldn't create the file." with "Ok";
  72. */
  73. lcreate(NumArgs,hFileName)
  74.  
  75. int NumArgs;
  76. HANDLE hFileName;
  77.  
  78. {
  79.     if (NumArgs != 1) return(STOP);
  80.     ReturnValue(itoh(create(deref(hFileName))));
  81.     return(STOP);
  82. }
  83.  
  84. /*
  85. ** This routine closes a file.
  86. **
  87. ** To call this handler from HyperPAD:
  88. **
  89. **    lclose fileNum;
  90. */
  91. lclose(NumArgs,hFileHandle)
  92.  
  93. int NumArgs;
  94. HANDLE hFileHandle;
  95.  
  96. {
  97.     if (NumArgs != 1) return(STOP);
  98.     close(htoi(hFileHandle));
  99.     return(STOP);
  100. }
  101.  
  102. /*
  103. ** This routine reads a specified number of bytes from a file
  104. ** and returns the text to HyperPAD. Be careful that the number of bytes
  105. ** is less than 32000 and that the data will not contain NULs.
  106. **
  107. **    put lread(fileNum,40) into page field 1;     -- read 40 bytes
  108. */
  109. lread(NumArgs,hFileHandle,hNumBytes)
  110.  
  111. int NumArgs;
  112. HANDLE hFileHandle;
  113. HANDLE hNumBytes;
  114.  
  115. {
  116.     WORD numbytes;
  117.     HANDLE hdl;
  118.  
  119.     if (NumArgs != 2) return(STOP);
  120.     numbytes = htoi(hNumBytes);
  121.     if (hdl = NewHandle(numbytes)) {
  122.         numbytes = read(htoi(hFileHandle),deref(hdl),numbytes);
  123.         ReAllocHandle(hdl,numbytes);
  124.         ReturnValue(hdl);
  125.     }
  126.     else ReturnValue(stoh(""));
  127.     return(STOP);
  128. }
  129.  
  130. /*
  131. ** This routine writes text to a file.
  132. **
  133. ** To call this handler from HyperPAD:
  134. **
  135. **    lwrite fileNum,"hello there";
  136. */
  137. lwrite(NumArgs,hFileHandle,hText)
  138.  
  139. int NumArgs;
  140. HANDLE hFileHandle,hText;
  141.  
  142. {
  143.     if (NumArgs != 2) return(STOP);
  144.     write(htoi(hFileHandle),deref(hText),strlen(deref(hText)));
  145.     return(STOP);
  146. }
  147.  
  148. /*
  149. ** This routine returns the number of bytes in an opened file.
  150. **
  151. ** To call this function from HyperPAD:
  152. **
  153. **    put lfilelength(fileNum) into page field 1;
  154. */
  155. lfilelength(NumArgs,hFileHandle)
  156.  
  157. int NumArgs;
  158. HANDLE hFileHandle;
  159.  
  160. {
  161.     DWORD d,c;
  162.     int handle;
  163.  
  164.     handle = htoi(hFileHandle);
  165.  
  166.     c = lseek(handle,0L,FROM_CURRENT);
  167.     d = lseek(handle,0L,FROM_EOF);
  168.     lseek(handle,d,FROM_BEGINNING);
  169.     ReturnValue(ltoh(d));
  170.     return(STOP);
  171. }
  172.  
  173. /*
  174. ** This routine returns the position of the file pointer within an opened file.
  175. **
  176. ** To call this function from HyperPAD:
  177. **
  178. **    put ltell(fileNum) into savedPosition;
  179. */
  180. ltell(NumArgs,hFileHandle)
  181.  
  182. int NumArgs;
  183. HANDLE hFileHandle;
  184.  
  185. {
  186.     if (NumArgs != 1) return(STOP);
  187.     ReturnValue(ltoh(lseek(htoi(hFileHandle),0L,FROM_CURRENT)));
  188.     return(STOP);
  189. }
  190.  
  191. /*
  192. ** This routine moves the file pointer anywhere within an opened file.
  193. **
  194. ** The handler takes three arguments:
  195. **
  196. **    fileNum        file number (returned from lopen())
  197. **    numBytes    number of bytes from the specified position
  198. **    fromWhere    "c" -> from current position
  199. **            "e" -> from end-of-file
  200. **            "b" -> from beginning of file
  201. **
  202. **                This parameter is case sensitive
  203. **
  204. ** To call this routine from HyperPAD:
  205. **
  206. **    lseek fileNum,200,"b";    -- seek 200 bytes from the start of the file
  207. */
  208. llseek(NumArgs,hFileHandle,hNumBytes,hFromWhere)
  209.  
  210. int NumArgs;
  211. HANDLE hFileHandle,hNumBytes,hFromWhere;
  212.  
  213. {
  214.     int from = FROM_BEGINNING;
  215.     PTR p;
  216.     DWORD d;
  217.  
  218.     if (NumArgs != 3) return(STOP);
  219.  
  220.     p = deref(hFromWhere);
  221.     if (*p == 'c') from = FROM_CURRENT;
  222.     else if (*p == 'e') from = FROM_EOF;
  223.     
  224.     ReturnValue(ltoh(lseek(htoi(hFileHandle),htol(hNumBytes),from)));
  225.     return(STOP);
  226. }
  227.  
  228. POOL pascal Pool[] = {
  229.     {    "lopen",    lopen,        0,    FUNCTION},
  230.     {    "lcreate",    lcreate,    0,    FUNCTION},
  231.     {    "lclose",    lclose,        0,    HANDLER},
  232.     {    "lread",    lread,        0,    FUNCTION},
  233.     {    "lwrite",    lwrite,        0,    HANDLER},
  234.     {    "lfilelength",    lfilelength,    0,    FUNCTION},
  235.     {    "ltell",    ltell,        0,    FUNCTION},
  236.     {    "lseek",    llseek,        0,    FUNCTION},
  237.     {    NULL,        NULL,        0,    0}        };
  238.